package com.tierstall.client.view.content;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.tierstall.client.presenter.GroupMGMTPresenter;
import com.tierstall.client.view.content.CV_showUsers.CwConstants;
import com.tierstall.client.view.main.ContentWidget;
import com.tierstall.shared.Page;
import com.tierstall.shared.UserType;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.TextBox;

public class CV_addGroup extends ContentWidget {
	public interface Display {
		// Presenter Methods here
		void onAddClicked(String genehmigung, String leiter);
	}

	private GroupMGMTPresenter presenter;

	public void setPresenter(GroupMGMTPresenter presenter) {
		this.presenter = presenter;
	}

	// standard stuff

	/**
	 * The UiBinder interface used by this example.
	 */
	interface Binder extends UiBinder<Widget, CV_addGroup> {
	}

	@UiField
	TextBox genehmigung;
	@UiField
	TextBox leiter;
	@UiField
	Button btn_add_group;

	// interface CView_addGroupUiBinder extends
	// UiBinder<Widget, CView_addGroup> {
	// }

	public CV_addGroup(CwConstants constants) {
		super(constants.vcAddGroupTitle(), constants.vcAddGroupDescription(),
				false, constants.vcAddGroupPermission(), constants
						.vcAddGroupPageName());
		this.constants = constants;
		// initWidget(uiBinder.createAndBindUi(this));
	}

	@UiHandler("btn_add_group")
	void onLoginClicked(ClickEvent event) {
		if (presenter != null) {
			presenter.onAddClicked(genehmigung.getText(), leiter.getText());
		}
	}

	public void succeeded() {
		genehmigung.setText("Group added successfully");
		genehmigung.setEnabled(false);
		leiter.setText("");
		btn_add_group.setEnabled(false);
	}

	public void failed() {
		genehmigung.setText("Group add failed...");
		leiter.setText("try again");

	}

	@Override
	public Widget onInitialize() {
		// Create the UiBinder.
		Binder uiBinder = GWT.create(Binder.class);
		Widget widget = uiBinder.createAndBindUi(this);

		return widget;
	}

	@Override
	protected void asyncOnInitialize(final AsyncCallback<Widget> callback) {
		GWT.runAsync(CV_addGroup.class, new RunAsyncCallback() {

			public void onFailure(Throwable caught) {
				callback.onFailure(caught);
			}

			public void onSuccess() {
				callback.onSuccess(onInitialize());
			}
		});

	}

	/**
	 * An instance of the constants.
	 */
	private CwConstants constants;

	/**
	 * The constants used in this Content Widget.
	 */
	public static interface CwConstants extends Constants {

		String vcAddGroupDescription();

		String vcAddGroupTitle();

		UserType vcAddGroupPermission();

		Page vcAddGroupPageName();
	}
}
